﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using AutoMapper;
using PublishNews.Entities;
using PublishNews.Models;
using PublishNews.Repositories;

namespace PublishNews.Controllers
{
    public class DistrictApiController : BaseApiController<District, DistrictModel>
    {
        private IDistrictRepository districtRepository;

        public DistrictApiController(IDistrictRepository repository)
            : base(repository)
        {
            districtRepository = repository;
        }

        public async Task<IHttpActionResult> GetDistrictsByLocationId(Guid locationId)
        {
            var entities = await districtRepository.GetDistrictsByLocationId(locationId);
            if (entities == null)
            {
                return NotFound();
            }

            var districts = Mapper.Map<List<DistrictModel>>(entities);
            districts = districts.OrderBy(x => x.Name).ToList();
            return Ok(districts);
        }

    }
}
